package org.lyq.erp.process.buyfulltemp.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.annotation.Resource;
import org.lyq.erp.config.json.JSONResult;
import org.lyq.erp.process.buyfulltemp.pojo.TblWxBuyfullTemplate;
import org.lyq.erp.process.buyfulltemp.service.TblWxBuyfullTemplateService;
import org.lyq.erp.utils.datetime.DateUtils;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "/TblWxBuyfullTemplate" })
public class TblWxBuyfullTemplateController {
	private static Logger logger = LoggerFactory.getLogger(TblWxBuyfullTemplateController.class);

	@Resource
	private TblWxBuyfullTemplateService tblWxBuyfullTemplateService;

	@RequestMapping(path = { "/insertAndUpdate" })
	public String insertAndUpdate(TblWxBuyfullTemplate buyfullTemp, String menuUuid, ModelMap map) {
		if (!insertValidation(buyfullTemp)) {
			buyfullTemp.popuperrorMessage = "true";
			map.put("dataBean", buyfullTemp);
			List<TblWxBuyfullTemplate> buyfullTempList = this.tblWxBuyfullTemplateService.selectAllBuyfullTemp();
			map.addAttribute("buyfullTempList", buyfullTempList);

			return "process/buyfulltemp/buyfulltemp_list";
		}
		try {
			if (buyfullTemp != null && buyfullTemp.getBuyfullTempId() != null) {
				this.tblWxBuyfullTemplateService.updateUserrankByPrimaryKeySelective(buyfullTemp);
			} else {
				this.tblWxBuyfullTemplateService.insertUserrankSelective(buyfullTemp);
			}
		} catch (Exception e) {

			logger.info(e.getMessage());
			buyfullTemp.addErrorMessage("buyfullTemp011");
			buyfullTemp.popuperrorMessage = "true";
			map.put("dataBean", buyfullTemp);
			List<TblWxBuyfullTemplate> buyfullTempList = this.tblWxBuyfullTemplateService.selectAllBuyfullTemp();
			map.addAttribute("buyfullTempList", buyfullTempList);
			return "process/buyfulltemp/buyfulltemp_list";
		}
		return "redirect:/TblWxBuyfullTemplate/list?menuUuid=" + menuUuid;
	}

	@RequestMapping({ "/delete" })
	public String delete(TblWxBuyfullTemplate buyfullTemp, String menuUuid, ModelMap map) {
		if (buyfullTemp.getBuyfullTempId() != null) {
			try {
				this.tblWxBuyfullTemplateService.deleteBuyfull(buyfullTemp.getBuyfullTempId());
				return "redirect:/TblWxBuyfullTemplate/list?menuUuid=" + menuUuid;
			} catch (Exception e) {
				logger.info(e.getMessage());
			}
		}

		buyfullTemp.addErrorMessage("buyfullTemp010");
		map.put("dataBean", buyfullTemp);
		List<TblWxBuyfullTemplate> buyfullTempList = this.tblWxBuyfullTemplateService.selectAllBuyfullTemp();
		map.addAttribute("buyfullTempList", buyfullTempList);
		return "process/buyfulltemp/buyfulltemp_list";
	}

	@RequestMapping({ "/list" })
	public String list(ModelMap map) {
		List<TblWxBuyfullTemplate> buyfullTempList = this.tblWxBuyfullTemplateService.selectAllBuyfullTemp();
		map.addAttribute("buyfullTempList", buyfullTempList);
		return "process/buyfulltemp/buyfulltemp_list";
	}

	@RequestMapping({ "/getBuyfullTemplate" })
	@ResponseBody
	public JSONResult getBuyfullTemplate(Integer buyfullTempId) {
		if (buyfullTempId != null) {

			TblWxBuyfullTemplate buyfullTemplate = this.tblWxBuyfullTemplateService
					.selectBuyfullTempByPrimaryKey(buyfullTempId);
			if (buyfullTemplate != null) {
				return JSONResult.ok(buyfullTemplate);
			}
			return JSONResult.errorMsg("buyfullTemp006");
		}

		return JSONResult.errorMsg("buyfullTemp006");
	}

	private boolean insertValidation(TblWxBuyfullTemplate buyfullTemp) {
		if (ObjectEmptyUtils.isEmptyString(buyfullTemp.getBuyfullTempName())) {
			buyfullTemp.addErrorMessage("buyfullTempName", "buyfullTemp001");
		}

		if (ObjectEmptyUtils.isEmptyString(buyfullTemp.getBuyfullTempDesc())) {
			buyfullTemp.addErrorMessage("buyfullTempDesc", "buyfullTemp002");
		}

		validationStartTimeAndEnaTime(buyfullTemp);

		if (ObjectEmptyUtils.isEmptyArray(buyfullTemp.getBuyfullAmt())) {
			buyfullTemp.addErrorMessage("buyfullTemp008");
		}

		if (ObjectEmptyUtils.isEmptyArray(buyfullTemp.getBuyfullSubtAmt())) {
			buyfullTemp.addErrorMessage("buyfullTemp009");
		}

		if (!ObjectEmptyUtils.isEmptyArray(buyfullTemp.getBuyfullAmt())
				&& !ObjectEmptyUtils.isEmptyArray(buyfullTemp.getBuyfullSubtAmt())) {
			if (buyfullTemp.getBuyfullAmt().length != buyfullTemp.getBuyfullSubtAmt().length) {
				if (buyfullTemp.getBuyfullAmt().length > buyfullTemp.getBuyfullSubtAmt().length) {
					buyfullTemp.addErrorMessage("buyfullTemp009");
				} else {
					buyfullTemp.addErrorMessage("buyfullTemp008");
				}
			} else {
				BigDecimal[] buyfullAmt = buyfullTemp.getBuyfullAmt();
				BigDecimal[] buyfullSubtAmt = buyfullTemp.getBuyfullSubtAmt();
				for (int i = 0; i < buyfullSubtAmt.length; i++) {
					if (buyfullAmt[i] == null) {
						buyfullTemp.addErrorMessage("buyfullTemp008");
					}
					if (buyfullSubtAmt[i] == null) {
						buyfullTemp.addErrorMessage("buyfullTemp009");
					}
				}
			}
		}
		return buyfullTemp.getMap().isEmpty();
	}

	private void validationStartTimeAndEnaTime(TblWxBuyfullTemplate buyfullTemp) {
		if (ObjectEmptyUtils.isEmptyString(buyfullTemp.getBuyfullTempStarttimeView())
				|| ObjectEmptyUtils.isEmptyString(buyfullTemp.getBuyfullTempEndtimeView())
				|| buyfullTemp.getBuyfullTempStarttimeView().trim().length() != 10
				|| buyfullTemp.getBuyfullTempEndtimeView().trim().length() != 10) {
			buyfullTemp.addErrorMessage("buyfullTempStarttimeView", "buyfullTemp003");
		} else if (!DateUtils.isThanToDay(buyfullTemp.getBuyfullTempStarttimeView().trim())) {
			buyfullTemp.addErrorMessage("buyfullTempStarttimeView", "buyfullTemp004");
		} else if (!DateUtils.endTimeMoreStartTime(buyfullTemp.getBuyfullTempEndtimeView().trim(),
				buyfullTemp.getBuyfullTempEndtimeView().trim())) {
			buyfullTemp.addErrorMessage("buyfullTempStarttimeView", "buyfullTemp005");
		}
	}
}
